/*
 * This is the class that holds information about the currently playing CD
 */
package edu.iyte.ceng431.hw03.cdplayer.model;

import java.util.ArrayList;

/**
 *
 * @author hp
 */
public class PlayingCD extends CD {

    private int index;
    private Track currentTrack;
    private boolean isPlaying;

    public PlayingCD(ArrayList<Track> tracks, String title) {
        super(tracks, title);
        index = 0;
        currentTrack = null;
        isPlaying = false;
    }

    //tops the playing or paused track
    public void stop() {
        currentTrack = null;
        isPlaying = false;
        setChanged();
        notifyObservers();
    }

    //plays the paused or stopped track or pauses the playing track
    public void playPause() {
        isPlaying = !isPlaying;
        if (currentTrack == null) {
            currentTrack = tracks.get(index);
        }
        setChanged();
        notifyObservers();
    }

    public boolean isPlaying() {
        return isPlaying;
    }

    //proceed to the next track to play
    public void nextTrack() {
        if (currentTrack == null && index == 0) {
            currentTrack = tracks.get(index);
        } else if (index < tracks.size() - 1) {
            currentTrack = tracks.get(++index);
        } else {
            index = 0;
            currentTrack = null;
        }
        setChanged();
        notifyObservers();
    }

    //proceed to the previous track to play
    public void previousTrack() {
        if (index > 0) {
            currentTrack = tracks.get(--index);
            setChanged();
            notifyObservers();
        }
    }

    /**
     * @return the currentTrack
     */
    public Track getCurrentTrack() {
        return currentTrack;
    }

    /**
     * @param currentTrack the currentTrack to set
     */
    public void setCurrentTrack(Track currentTrack) {
        this.currentTrack = currentTrack;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
